package com.newtype.meeting.client.widget.shared;

import java.util.Map;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.CheckChangedEvent;
import com.extjs.gxt.ui.client.event.CheckChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * 部门可单选窗口
 * @author 汪恭泽
 * Jun 17, 2011 1:23:30 PM
 */
public class DeptTreeSingleSelectWindow extends Window {
	private TreePanel<ModelData> tree;
	private ListStore<ModelData> listStore;
	private ListView<ModelData> view;
	private TreeStore<ModelData> treeStore = new TreeStore<ModelData>();

	public DeptTreeSingleSelectWindow(final Map<String, Field<String>> pDept) {
		setSize(450, 500);
		setModal(true);
		setLayout(new BorderLayout());
		setHeading("选择部门");

		createWest();
		createCenter();

		Button btnConfirm = new Button("确定选取");
		btnConfirm.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				ModelData model = listStore.getModels().get(0);

				pDept.get("pDeptID").setValue((String) model.get("id"));
				pDept.get("pDeptName").setValue((String) model.get("name"));
				hide();
			}
		});
		Button btnCancel = new Button("取消");
		btnCancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				listStore.removeAll();
				hide();
			}
		});
		addButton(btnConfirm);
		addButton(btnCancel);
	}

	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 225);
		westData.setMargins(new Margins(5, 0, 5, 5));
		ContentPanel westPanel = new ContentPanel();
		westPanel.setHeaderVisible(false);

		tree = new TreePanel<ModelData>(treeStore);
		tree.setDisplayProperty("name");
		tree.setCheckStyle(CheckCascade.NONE);
		tree.setIconProvider(new TreeModelIconProvider());//显示图标
		tree.setCheckable(true);
		tree.expandAll();
		tree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		listStore = new ListStore<ModelData>();

		tree.addCheckListener(new CheckChangedListener<ModelData>() {
			@Override
			public void checkChanged(CheckChangedEvent<ModelData> event) {
				listStore.removeAll();
				if (event.getCheckedSelection().size() > 0) {
					listStore.add(event.getCheckedSelection().get(0));
				}
			}
		});

		westPanel.add(tree);

		add(westPanel, westData);
	}

	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeading("已选部门");

		view = new ListView<ModelData>() {
			@Override
			protected ModelData prepareData(ModelData model) {
				String name = model.get("name");
				model.set("shortName", Format.ellipse(name, 15));
				return model;
			}
		};

		view.setStore(listStore);
		view.setDisplayProperty("shortName");
		view.setBorders(false);

		centerPanel.add(view);
		add(centerPanel, centerData);
	}

	public TreeStore<ModelData> getTreeStore() {
		return treeStore;
	}

	/**
	 * 图标显示
	 * @author 汪恭泽
	 *
	 */
	public class TreeModelIconProvider implements ModelIconProvider<ModelData> {
		@Override
		public AbstractImagePrototype getIcon(ModelData model) {
			return IconHelper.createPath(GWT.getHostPageBaseURL()
					+ "images/icons/dept.gif");
		}
	}

}
